package com.loubobooo.personal.blog.controller.backend;

import com.loubobooo.personal.blog.entity.Category;
import com.loubobooo.personal.blog.entity.vo.CategoryVO;

import com.loubobooo.personal.blog.service.ArticleService;
import com.loubobooo.personal.blog.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller @RequestMapping("/admin/category") public class CategoryManageController {
    @Autowired private ArticleService articleService;

    @Autowired private CategoryService categoryService;

    //后台分类列表显示
    @RequestMapping(value = "") public ModelAndView categoryList() {
        ModelAndView modelandview = new ModelAndView();

        List<CategoryVO> categoryVOList = categoryService.listCategory(null);
        modelandview.addObject("categoryVOList", categoryVOList);

        modelandview.setViewName("Admin/Category/index");
        return modelandview;

    }

    //后台添加分类提交
    @RequestMapping(value = "/insertSubmit", method = RequestMethod.POST) public String insertCategorySubmit(
            Category category) {
        categoryService.insertCategory(category);
        return "redirect:/admin/category";
    }

    //删除分类
    @RequestMapping(value = "/delete/{id}") public String deleteCategory(@PathVariable("id") Integer id) {

        //禁止删除有文章的分类
        int count = articleService.countArticleWithCategory(null, id);
        if (count == 0) {
            categoryService.deleteCategory(id);
        }

        return "redirect:/admin/category";
    }

    //编辑分类页面显示
    @RequestMapping(value = "/edit/{id}") public ModelAndView editCategoryView(@PathVariable("id") Integer id) {
        ModelAndView modelAndView = new ModelAndView();

        CategoryVO categoryVO = categoryService.getCategoryById(null, id);
        modelAndView.addObject("categoryVO", categoryVO);

        List<CategoryVO> categoryVOList = categoryService.listCategory(null);
        modelAndView.addObject("categoryVOList", categoryVOList);

        modelAndView.setViewName("Admin/Category/edit");
        return modelAndView;
    }

    //编辑分类提交
    @RequestMapping(value = "/editSubmit", method = RequestMethod.POST) public String editCategorySubmit(
            Category category) {
        categoryService.updateCategory(category);
        return "redirect:/admin/category";
    }
}
